package com.io.buffered;



import java.io.*;

/**
 * 字节流文件拷贝+缓冲流  提升性能
 *
 * @author jiangbang
 * @date 2016/6/2
 */
public class BufferedByteDemo {

    /**
     * 文件拷贝
     */
    public static  void test3(){
        //1.建立联系 服了对象 目的地
        File file= new File("D:/demo/1.txt");
        File file2= new File("D:/demo/2.txt");
        //2.选择流
        OutputStream os = null;//文件输出流  outputstream FileOutputStream
        InputStream is = null;//文件输日流  InputStream FileInputStream
        try {
            //拷贝操作
            is = new BufferedInputStream(new FileInputStream(file));
            os = new BufferedOutputStream(new FileOutputStream(file2));

            int len = 0;
            byte[] bytes = new byte[100];
            while(-1!=(len=is.read(bytes))){
                os.write(bytes,0,len);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        test3();
    }
}
